<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,deweb.cimaplanning.*,java.text.DecimalFormat,com.cs.common.utils.NumberUtil"%>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<style>
#ajustslayer {
	position: relative;
	visibility: visible;
	width: 950px;
	height: 430px;
	background-color: #FFFFFF;
	border: 1px solid #CCCCCC;
	padding: 10px;
	overflow-x:scroll;
	overflow-y:scroll;
	overflow:moz-scrollbars-horizontal; 
}
</style>
<div id="ajustslayer" align="left">
	<table>
		<tr>
			<td align="left" class="titulo_lista_interna">
				<html:button property="distribution" onclick="location.href ='saveDistributionPOToPlants.do'" styleClass="boton"><bean:message key="label.versions.future.distribution.version.distribution"/></html:button>&nbsp;
				<html:button property="delDistribution" onclick="location.href ='deleteDistributionPOToPlants.do'" styleClass="boton_large"><bean:message key="label.versions.future.distribution.version.deldistribution"/></html:button>&nbsp;
				<html:button property="back" onclick="location.href ='showVersionDetail.do'" styleClass="boton"><bean:message key="label.common.back"/></html:button>
			</td>
		</tr>
		<tr>
	    	<td>
<%
boolean DEBUG = false;
if (DEBUG) out.println("<br>DEBUG ON: [" + request.getRequestURI() + "]");

boolean read = true;//seg.getPermEnt(misc.APP, misc.M_CATALOG, misc.S_CASETA, misc.E_CASETA, com.deweb.acpmodseg.ACPModSeg.PERM_LECTURA);
boolean edit = true;//seg.getPermEnt(misc.APP, misc.M_CATALOG, misc.S_CASETA, misc.E_CASETA, com.deweb.acpmodseg.ACPModSeg.PERM_ADMIN);

if (DEBUG) out.println("<br>Permiso lectura: [" + read + "]");
if (DEBUG) out.println("<br>Permiso administracion: [" + edit + "]");

if (!read){
	out.println("No tiene permiso de acceso a esta Área");
	out.println("<br><a href=\"javascript:history.back();\">Regresar</a>");
	return;
}

com.deweb.GetProperties prop;
Connection conn 		= null;
Statement stmt  		= null;
Statement stmtPlants  	= null;
Statement stmtCornInventories  	= null;
ResultSet rs			= null;
ResultSet rsPlants		= null;

String sql 		= null;


try{
    prop 				= new com.deweb.GetProperties(request);
    conn 				= prop.getDBConnection();
    stmt 				= conn.createStatement();
    stmtPlants 			= conn.createStatement();
    stmtCornInventories = conn.createStatement();
	
	sql = "";
	
	String keyId	= request.getParameter("keyId") == null ? "" : request.getParameter("keyId");
	int frmAction	= request.getParameter("frmAction") == null ? 0 : (request.getParameter("frmAction").toString().length() == 0 ? 0 : Integer.parseInt(request.getParameter("frmAction").toString()));
	boolean print	= request.getParameter("print") == null ? false : (request.getParameter("frmAction").toString().equals("1") ? true : false);
	
	int order	= request.getParameter("order") == null ? 1 : (request.getParameter("order").toString().length() == 0 ? 1 : Integer.parseInt(request.getParameter("order").toString()));
	String dir	= request.getParameter("dir") == null ? misc.ASC : request.getParameter("dir");
	String versionID = request.getParameter("versionId") == null ? (session.getAttribute("versionId")==null?"":(String)session.getAttribute("versionId")): request.getParameter("versionId");
	String recalculateNow = request.getParameter("recalculate_now") == null ? "": request.getParameter("recalculate_now");
	if (DEBUG) {
		out.println("<br>keyId: [" + keyId + "]");
		out.println("<br>frmAction: [" + frmAction + "]");
		out.println("<br>order: [" + order + "]");
		out.println("<br>dir: [" + dir + "]");
	} 
	
	sql = "select count(*) as total from monthly_distribution_per_plant Where version_id = " + versionID;
	rs = stmt.executeQuery(sql);
	if( rs.next() ){
		if(rs.getInt("total") > 0){
	
			if (versionID.length() == 0) return;
			sql  = " Select distinct * "; 
			sql  +=	" 	from vw_monthly_version_distribution2 "; 
			sql  +=	" 	where version_id = " + versionID;
			sql  +=	" 	order by current_year, current_month, po_year,po_month,biweek, code, code_number, plant_short_name";
		
			
		  if (DEBUG) out.println("<br>sql: [" + sql + "]");
		    rs = stmt.executeQuery(sql);
			
			int setOrder = 0;
			boolean odd = false;
			int counter	= 0; 
			
			String id 	= "";
			String dsc	= "";
			
		    DecimalFormat df = new DecimalFormat("#,##0.000");
		
		
		
			
			int prevYear  = 0;
			int prevMonth = 0;
			String prevPONum = "";
			double[] plantsDistributed = new double[3];
			String[][] plantsHeader  = new String [10][4];
			String[][] cornDistribution  = new String [4][4];
		
			double POCapacity =0;
			double POInitial =0;
			double PODistributed = 0;
			double POBlocked = 0;
			boolean completeCurrentTable = false;
			out.println("                    	<table border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"  bgcolor=\"#CCCCC0\">");
		    out.println("                        <tr>");
		    out.println("                         <td>");
			out.println("                    		<table border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"1\"  bgcolor=\"#CCCCC0\">");
		    out.println("                            <tr class=\"titulocolumna\">");
		    out.println("                              <td width=\"100\">PO</td>");
		    out.println("                              <td width=\"100\">Inv.&nbsp;Inicial</td>");
		    out.println("                              <td width=\"100\">Compras</td>");
		    out.println("                              <td width=\"100\">Disponible</td>");
		    out.println("                  			   <td width=\"100\">PPA</td>");
		    out.println("                              <td width=\"100\">PSJ</td>");
		    out.println("                              <td width=\"100\">PTL</td>");
		    out.println("                              <td NOWRAP>Inv. Final</td>");
		    out.println("                            </tr>");
		    odd = false;
		    
			while (rs.next()) {
				odd = !odd;
				counter++;
				
				String poNum = rs.getString("Code").trim()+ "000".substring(1, 4 - rs.getString("code_number").length()) + rs.getString("code_number");
				if ( ((prevMonth != rs.getInt("current_month")) || (prevYear != rs.getInt("current_year"))) && (prevMonth == 0)){
					out.println("                <tr class=\"" + (odd ? "columnadatoPar" : "columnadatoNon") + "\">" );
					out.println("                  <td align=\"center\" colspan=\"8\">"+ rs.getInt("current_year") +" - " + rs.getInt("current_month") +"</td>");
					out.println("                </tr>");
					odd = !odd;
					
					sql = "select v.version_id,mp.[year],mp.[month], p.short_name as plant_short_name,p.description as plant_desciption, pv.*, ISNULL(vpvg.total_grinded,0) as total_grinded";
					sql += " FROM 		versions v ";
					sql += " 			inner join dbo.months_periods_versions	mpv	on mpv.version_id = v.version_id"; 
					sql += " 			inner join dbo.plants_versions	pv	on mpv.month_period_version_id = pv.month_period_version_id"; 
					sql += " 			inner join dbo.plants				p	on p.plant_id = pv.plant_id";
					sql += " 			inner join dbo.months_periods			mp	on mp.month_period_id = mpv.month_period_id";
					sql += " 			inner join dbo.vw_plant_version_grind		vpvg	on vpvg.plant_version_id = pv.plant_version_id";
					sql += " where v.version_id = " + versionID; 
					sql += " 	and mp.[year] = " 	+ rs.getInt("current_year"); 
					sql += " 	and mp.[month] = " 	+ rs.getInt("current_month");
					rsPlants = stmtPlants.executeQuery(sql);
					plantsHeader  = new String [10][4];
					int plantsArrayId =0;
		
					plantsHeader [0][0] = "Programa de molienda"; 
					plantsHeader [1][0] = "Días molienda Inv. Ini.";
					plantsHeader [2][0] = "Almacen";
					plantsHeader [3][0] = "Inv. Inicial Plantas";
					plantsHeader [4][0] = "Inv. Final Plantas";
					plantsHeader [5][0] = "Max Req";
					plantsHeader [6][0] = "Distribuido";
					plantsHeader [7][0] = "Molido";
					plantsHeader [8][0] = "Descarga";
					while (rsPlants.next()){
						if 	(rsPlants.getString("plant_short_name").trim().equals("PPA")) plantsArrayId = 1;
						if 	(rsPlants.getString("plant_short_name").trim().equals("PSJ")) plantsArrayId = 2;
						if 	(rsPlants.getString("plant_short_name").trim().equals("PTL")) plantsArrayId = 3;
						plantsHeader [0][plantsArrayId] = df.format(rsPlants.getDouble("grind") ); 
						plantsHeader [1][plantsArrayId] = df.format(rsPlants.getDouble("grind_protection_days"));
						plantsHeader [2][plantsArrayId] = df.format(rsPlants.getDouble("storage_amount"));
						plantsHeader [3][plantsArrayId] = df.format(rsPlants.getDouble("start_inventory"));
						plantsHeader [4][plantsArrayId] = df.format(rsPlants.getDouble("end_inventory"));
						plantsHeader [5][plantsArrayId] = df.format(rsPlants.getDouble("grind") +  rsPlants.getDouble("storage_amount") - (rsPlants.getDouble("start_inventory") == 0.0 ? 0.0 :rsPlants.getDouble("start_inventory")));
						plantsHeader [6][plantsArrayId] = df.format(rsPlants.getDouble("received"));
						plantsHeader [7][plantsArrayId] = df.format(rsPlants.getDouble("total_grinded"));
						plantsHeader [8][plantsArrayId] = df.format(rsPlants.getDouble("unload_amount"));
		
					}
					for (int i = 0; i<9; i++){
						out.println("                <tr class=\"" + (odd ? "columnadatoPar" : "columnadatoNon") + "\">" );
						out.println("                  <td align=\"left\" colspan=\"4\">"+ plantsHeader [i][0] +"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ plantsHeader [i][1]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ plantsHeader [i][2]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ plantsHeader [i][3]+"</td>");
						out.println("                  <td align=\"center\" colspan=\"1\">&nbsp;</td>");
						out.println("                </tr>");
						odd = !odd;
					}
					out.println("                <tr class=\"" + (odd ? "columnadatoPar" : "columnadatoNon") + "\">" );
					out.println("                  <td align=\"center\" colspan=\"8\">&nbsp;</td>");
					out.println("                </tr>");
					
					/*
						Maiz por planta
					*/
					
					sql = "Select * ";
					sql += " from vw_corn_type_distributions "; 
					sql += " where version_id = " + versionID; 
					sql += " 	and [year] = " 	+ rs.getInt("current_year"); 
					sql += " 	and [month] = " 	+ rs.getInt("current_month");
					sql += " order by [year],[month],corn_type_id,plant_id";
					ResultSet rsCornTypeInv = stmtCornInventories.executeQuery(sql);
						out.println("                <tr class=\"titulocolumna\">" );
					out.println("                  <td align=\"left\"colspan=\"2\" >Maiz</td>");
					out.println("                  <td align=\"left\"colspan=\"2\" >&nbsp;</td>");
					out.println("                  <td align=\"right\" >PPA</td>");
					out.println("                  <td align=\"right\" >PSJ</td>");
					out.println("                  <td align=\"right\" >PTL</td>");
					out.println("                  <td align=\"center\" >&nbsp;</td>");
					out.println("                </tr>" );
					
					odd = true;
					cornDistribution[0][0] = "Inv. Inicial";
					cornDistribution[1][0] = "Distribuido";
					cornDistribution[2][0] = "Molido";
					cornDistribution[3][0] = "Inv. Final";
					String prevCornType = "";
					while (rsCornTypeInv.next()){
						if 	(rsCornTypeInv.getString("short_name").trim().equals("PPA")) plantsArrayId = 1;
						if 	(rsCornTypeInv.getString("short_name").trim().equals("PSJ")) plantsArrayId = 2;
						if 	(rsCornTypeInv.getString("short_name").trim().equals("PTL")) plantsArrayId = 3;
						
						if ( (prevCornType.length() > 0) && (!prevCornType.equals(rsCornTypeInv.getString("code")))){
							
							for (int i = 0; i<4; i++){
								out.println("                <tr class=\"" + (odd ? "columnadatoPar" : "columnadatoNon") + "\">" );
								if (i == 0)
									out.println("                  <td align=\"center\" colspan=\"2\">"+ prevCornType +"</td>");
								else
									out.println("                  <td align=\"center\" colspan=\"2\">&nbsp;</td>");
								out.println("                  <td align=\"left\" colspan=\"2\">"+ cornDistribution [i][0] +"</td>");
								out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][1]+"</td>");
								out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][2]+"</td>");
								out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][3]+"</td>");
								out.println("                  <td align=\"right\" colspan=\"1\">&nbsp;</td>");
								out.println("                </tr>");
							}
							odd = !odd;
							
						}
						cornDistribution[0][plantsArrayId] = df.format(rsCornTypeInv.getDouble("start_inventory"));
						cornDistribution[1][plantsArrayId] = df.format(rsCornTypeInv.getDouble("distributed"));
						cornDistribution[2][plantsArrayId] = df.format(rsCornTypeInv.getDouble("grinded"));
						cornDistribution[3][plantsArrayId] = df.format(rsCornTypeInv.getDouble("end_inventory"));
						
						prevCornType =  rsCornTypeInv.getString("code");
					}
					for (int i = 0; i<4; i++){
						out.println("                <tr class=\"" + (odd ? "columnadatoPar" : "columnadatoNon") + "\">" );
						if (i == 0)
							out.println("                  <td align=\"center\" colspan=\"2\">"+ prevCornType +"</td>");
						else
							out.println("                  <td align=\"center\" colspan=\"2\">&nbsp;</td>");
						out.println("                  <td align=\"left\" colspan=\"2\">"+ cornDistribution [i][0] +"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][1]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][2]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][3]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">&nbsp;</td>");
						out.println("                </tr>");
					}
					
					odd = true;
		
				}
				
				if (prevPONum.length() == 0) prevPONum = poNum;			
		
				if (!poNum.equals(prevPONum))	{
					out.println("                <tr class=\"" + (odd ? "columnadatoPar" : "columnadatoNon") + "\">");
					out.println("                  <td>" + prevPONum + "</td>");
					out.println("                  <td align=\"right\">" + (POInitial >0.0 ? df.format(POInitial): "&nbsp;") + "</td>");
					out.println("                  <td align=\"right\">" + (POCapacity >0.0 ? df.format(POCapacity): "&nbsp;") + "</td>");
					out.println("                  <td align=\"right\">" + (POCapacity + POInitial > 0.0 ? df.format(POCapacity + POInitial) : "&nbsp;") + "</td>");
					out.println("                  <td align=\"right\">" + (plantsDistributed[0] > 0.0 ? df.format(plantsDistributed[0]) : "&nbsp;") + "</td>");
					out.println("                  <td align=\"right\">" + (plantsDistributed[1] > 0.0 ? df.format(plantsDistributed[1]) : "&nbsp;") + "</td>");
					out.println("                  <td align=\"right\">" + (plantsDistributed[2] > 0.0 ? df.format(plantsDistributed[2]) : "&nbsp;") + "</td>");
					out.println("                  <td align=\"right\">" + ( (POCapacity + POInitial)- PODistributed > 0.0 ? df.format((POCapacity + POInitial)- PODistributed) : "&nbsp;") + "</td>");
					out.println("                </tr>");
					odd = !odd;
					plantsDistributed[0] =0;
					plantsDistributed[1] =0;
					plantsDistributed[2] =0;
					POCapacity 	= 0;
					POInitial 	= 0;
					if ((rs.getInt("current_year") == rs.getInt("po_year")) 
					 && (rs.getInt("current_month") == rs.getInt("po_month")))
						POCapacity = (rs.getDouble("capacity") );
					else
						POInitial  = rs.getDouble("capacity");
					POBlocked = rs.getDouble("blocked_qty");
					PODistributed = rs.getDouble("total_distributed_per_month");
				}
		
		//		if  (POCapacity == 0)  POCapacity = (rs.getDouble("capacity") );
					POCapacity 	= 0;
					POInitial 	= 0;
					if ((rs.getInt("current_year") == rs.getInt("po_year")) 
					 && (rs.getInt("current_month") == rs.getInt("po_month")))
						POCapacity = (rs.getDouble("capacity") );
					else
						POInitial  = rs.getDouble("capacity");
				if  (POBlocked == 0)  POBlocked = rs.getDouble("blocked_qty");
				if  (PODistributed == 0)  PODistributed = rs.getDouble("total_distributed_per_month");
				
				
				if 	((rs.getString("plant_short_name").trim().equals("PPA")) 
						&& (rs.getString("plant_distributed").trim().equals("PPA")) 
		/*				&& (rs.getInt("current_year") == rs.getInt("po_year")) 
						&& (rs.getInt("current_month") == rs.getInt("po_month")) */) 
					plantsDistributed[0] += rs.getDouble("distributed");
				if 	((rs.getString("plant_short_name").trim().equals("PSJ")) 
						&& (rs.getString("plant_distributed").trim().equals("PSJ")) 
		/*				&& (rs.getInt("current_year") == rs.getInt("po_year")) 
						&& (rs.getInt("current_month") == rs.getInt("po_month")) */) 
					plantsDistributed[1] += rs.getDouble("distributed");
				if 	((rs.getString("plant_short_name").trim().equals("PTL")) 
						&& (rs.getString("plant_distributed").trim().equals("PTL")) 
		/*				&& (rs.getInt("current_year") == rs.getInt("po_year")) 
						&& (rs.getInt("current_month") == rs.getInt("po_month")) */) 
					plantsDistributed[2] += rs.getDouble("distributed");
		
				if ( ((prevMonth != rs.getInt("current_month")) || (prevYear != rs.getInt("current_year"))) && (prevMonth >0)){
		        	out.println("                </table>");
		            out.println("               </td>");
		            out.println("               <td>");
		        	
		        	out.println("                    		<table border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"1\">");
		            out.println("                            <tr class=\"titulocolumna\">");
		            out.println("                              <td>PO</td>");
		            out.println("                              <td>Inv.&nbsp;Inicial</td>");
		            out.println("                              <td>Compras</td>");
		            out.println("                              <td>Disponible</td>");
		            out.println("                  			   <td>PPA</td>");
		            out.println("                              <td>PSJ</td>");
		            out.println("                              <td>PTL</td>");
		            out.println("                              <td NOWRAP>Inv. Final</td>");
		            out.println("                            </tr>");
					out.println("                <tr class=\"" + (odd ? "columnadatoPar" : "columnadatoNon") + "\">" );
					out.println("                  <td align=\"center\" colspan=\"8\">"+ rs.getInt("current_year") +" - " + rs.getInt("current_month") +"</td>");
					out.println("                </tr>");
					
					sql = "select v.version_id,mp.[year],mp.[month], p.short_name as plant_short_name,p.description as plant_desciption, pv.*";
					sql = "select v.version_id,mp.[year],mp.[month], p.short_name as plant_short_name,p.description as plant_desciption, pv.*, vpvg.total_grinded";
					sql += " FROM 		versions v ";
					sql += " 			inner join dbo.months_periods_versions	mpv	on mpv.version_id = v.version_id"; 
					sql += " 			inner join dbo.plants_versions	pv	on mpv.month_period_version_id = pv.month_period_version_id"; 
					sql += " 			inner join dbo.plants				p	on p.plant_id = pv.plant_id";
					sql += " 			inner join dbo.months_periods			mp	on mp.month_period_id = mpv.month_period_id";
					sql += " 			inner join dbo.vw_plant_version_grind		vpvg	on vpvg.plant_version_id = pv.plant_version_id";
					sql += " where v.version_id = " + versionID; 
					sql += " 	and mp.[year] = " 	+ rs.getInt("current_year"); 
					sql += " 	and mp.[month] = " 	+ rs.getInt("current_month");
					rsPlants = stmtPlants.executeQuery(sql);
					plantsHeader  = new String [10][4];
					int plantsArrayId =0;
		
					plantsHeader [0][0] = "Programa de molienda"; 
					plantsHeader [1][0] = "Días molienda Inv. Ini.";
					plantsHeader [2][0] = "Almacen";
					plantsHeader [3][0] = "Inv. Inicial Plantas";
					plantsHeader [4][0] = "Inv. Final Plantas";
					plantsHeader [5][0] = "Max Req";
					plantsHeader [6][0] = "Distribuido";
					plantsHeader [7][0] = "Molido";
					plantsHeader [8][0] = "Descarga";
					while (rsPlants.next()){
						if 	(rsPlants.getString("plant_short_name").trim().equals("PPA")) plantsArrayId = 1;
						if 	(rsPlants.getString("plant_short_name").trim().equals("PSJ")) plantsArrayId = 2;
						if 	(rsPlants.getString("plant_short_name").trim().equals("PTL")) plantsArrayId = 3;
						plantsHeader [0][plantsArrayId] = df.format(rsPlants.getDouble("grind") ); 
						plantsHeader [1][plantsArrayId] = df.format(rsPlants.getDouble("grind_protection_days"));
						plantsHeader [2][plantsArrayId] = df.format(rsPlants.getDouble("storage_amount"));
						plantsHeader [3][plantsArrayId] = df.format(rsPlants.getDouble("start_inventory"));
						plantsHeader [4][plantsArrayId] = df.format(rsPlants.getDouble("end_inventory"));
						plantsHeader [5][plantsArrayId] = df.format(rsPlants.getDouble("grind") +  rsPlants.getDouble("storage_amount") - rsPlants.getDouble("start_inventory"));
						plantsHeader [6][plantsArrayId] = df.format(rsPlants.getDouble("received"));
						plantsHeader [7][plantsArrayId] = df.format(rsPlants.getDouble("total_grinded"));
						plantsHeader [8][plantsArrayId] = df.format(rsPlants.getDouble("unload_amount"));
		
					}
					for (int i = 0; i<9; i++){
						out.println("                <tr class=\"" + (odd ? "columnadatoPar" : "columnadatoNon") + "\">" );
						out.println("                  <td align=\"left\" colspan=\"4\">"+ plantsHeader [i][0] +"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ plantsHeader [i][1]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ plantsHeader [i][2]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ plantsHeader [i][3]+"</td>");
						out.println("                  <td align=\"center\" colspan=\"1\">&nbsp;</td>");				
						out.println("                </tr>");
						odd = !odd;
					}
					out.println("                <tr class=\"" + (odd ? "columnadatoPar" : "columnadatoNon") + "\">" );
					out.println("                  <td align=\"center\" colspan=\"8\">&nbsp;</td>");
					out.println("                </tr>");
					odd = true;
					/*
						Maiz por planta
					*/
					
					sql = "Select * ";
					sql += " from vw_corn_type_distributions "; 
					sql += " where version_id = " + versionID; 
					sql += " 	and [year] = " 	+ rs.getInt("current_year"); 
					sql += " 	and [month] = " 	+ rs.getInt("current_month");
					sql += " order by [year],[month],corn_type_id,plant_id";
					ResultSet rsCornTypeInv = stmtCornInventories.executeQuery(sql);
					out.println("                <tr class=\"titulocolumna\">" );
					out.println("                  <td align=\"left\" colspan=\"2\">Maiz</td>");
					out.println("                  <td align=\"left\" colspan=\"2\">&nbsp;</td>");
					out.println("                  <td align=\"right\" >PPA</td>");
					out.println("                  <td align=\"right\" >PSJ</td>");
					out.println("                  <td align=\"right\" >PTL</td>");
					out.println("                  <td align=\"center\" >&nbsp;</td>");
					out.println("                </tr>" );
					
					
					cornDistribution[0][0] = "Inv. Inicial";
					cornDistribution[1][0] = "Distribuido";
					cornDistribution[2][0] = "Molido";
					cornDistribution[3][0] = "Inv. Final";
					String prevCornType = "";
					while (rsCornTypeInv.next()){
						if 	(rsCornTypeInv.getString("short_name").trim().equals("PPA")) plantsArrayId = 1;
						if 	(rsCornTypeInv.getString("short_name").trim().equals("PSJ")) plantsArrayId = 2;
						if 	(rsCornTypeInv.getString("short_name").trim().equals("PTL")) plantsArrayId = 3;
						
						if ( (prevCornType.length() > 0) && (!prevCornType.equals(rsCornTypeInv.getString("code")))){
							
							for (int i = 0; i<4; i++){
								out.println("                <tr class=\"" + (odd ? "columnadatoPar" : "columnadatoNon") + "\">" );
								if (i == 0)
									out.println("                  <td align=\"center\" colspan=\"2\">"+ prevCornType +"</td>");
								else
									out.println("                  <td align=\"center\" colspan=\"2\">&nbsp;</td>");
								out.println("                  <td align=\"left\" colspan=\"2\">"+ cornDistribution [i][0] +"</td>");
								out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][1]+"</td>");
								out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][2]+"</td>");
								out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][3]+"</td>");
								out.println("                  <td align=\"right\" colspan=\"1\">&nbsp;</td>");
								out.println("                </tr>");
							}
							odd = !odd;
							
						}
						cornDistribution[0][plantsArrayId] = df.format(rsCornTypeInv.getDouble("start_inventory"));
						cornDistribution[1][plantsArrayId] = df.format(rsCornTypeInv.getDouble("distributed"));
						cornDistribution[2][plantsArrayId] = df.format(rsCornTypeInv.getDouble("grinded"));
						cornDistribution[3][plantsArrayId] = df.format(rsCornTypeInv.getDouble("end_inventory"));
						
						prevCornType =  rsCornTypeInv.getString("code");
					}
					for (int i = 0; i<4; i++){
						out.println("                <tr class=\"" + (odd ? "columnadatoPar" : "columnadatoNon") + "\">" );
						if (i == 0)
							out.println("                  <td align=\"center\" colspan=\"2\">"+ prevCornType +"</td>");
						else
							out.println("                  <td align=\"center\" colspan=\"2\">&nbsp;</td>");
						out.println("                  <td align=\"left\" colspan=\"2\">"+ cornDistribution [i][0] +"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][1]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][2]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">"+ cornDistribution [i][3]+"</td>");
						out.println("                  <td align=\"right\" colspan=\"1\">&nbsp;</td>");
						out.println("                </tr>");
					}
						
					odd = true;
			
					
				}
				prevMonth = rs.getInt("current_month");
				prevYear  = rs.getInt("current_year");
				prevPONum = poNum;	
			}
			
			out.println("                <tr class=\"" + (odd ? "columnadatoPar" : "columnadatoNon") + "\">");
			out.println("                  <td>" + prevPONum + "</td>");
			out.println("                  <td align=\"right\">" + (POInitial >0.0 ? df.format(POInitial): "&nbsp;") + "</td>");
			out.println("                  <td align=\"right\">" + (POCapacity >0.0 ? df.format(POCapacity): "&nbsp;") + "</td>");
			out.println("                  <td align=\"right\">" + (POCapacity + POInitial > 0.0 ? df.format(POCapacity + POInitial) : "&nbsp;") + "</td>");
			out.println("                  <td align=\"right\">" + (plantsDistributed[0] > 0.0 ? df.format(plantsDistributed[0]) : "&nbsp;") + "</td>");
			out.println("                  <td align=\"right\">" + (plantsDistributed[1] > 0.0 ? df.format(plantsDistributed[1]) : "&nbsp;") + "</td>");
			out.println("                  <td align=\"right\">" + (plantsDistributed[2] > 0.0 ? df.format(plantsDistributed[2]) : "&nbsp;") + "</td>");
			out.println("                  <td align=\"right\">" + ((POCapacity + POInitial)- PODistributed > 0.0 ? df.format((POCapacity + POInitial)- PODistributed)  : "&nbsp;") + "</td>");
			out.println("                </tr>");
			out.println("                </table>");
		    out.println("               </td>");
		    out.println("               <td>");
			out.println("</td>");
			out.println("</tr>");
			out.println("</table>");
			
		}
	}
}catch (Exception e){
    out.println("<hr><br>Error Description: " + e.getMessage() + "<hr>");
} finally {
    try { conn.close(); } catch (Exception e) {}
    try { stmt.close(); } catch (Exception e) {}
    try { rs.close();   } catch (Exception e) {}
}	
%></td>
	  </tr>
	</table>
</div>